﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Designer;
using DevComponents.DotNetBar;
using TextEditor.TextDocument.Data;
using TextEditor.TextDocument.Data.Knowledge;
using TextEditor.TextDocument.Document;
using ZYCommon;

namespace TextEditor
{
    public class UcEditorControl : ZYViewControl
    {
        private bool bolEnableSmartTag = true;
        public ZYKBTreeView KBTreeView = null;
        protected ZYTextDocument myEMRDoc = null;
        private System.Windows.Forms.ImageList myImageList;
        protected ZYKBPopupList myKBPopupList = null;
        public ContextMenuStrip myPopupMenu;
        private ArrayList mySmartTagProviderList;
        private PictureBox myTagBox;
        private ContextMenu myTagMenu;
        public bool RunInDesigner = false;
        private string strCurrentID = null;
        private string strCurrentUserName = null;
        private string strKBBufferFile = null;
        public frKnowledgeSelect mySelectedKb = null;          //知识库列表  
        public frmInputTimeUpdate myInputTimeUpdate = null;
        public timeManager myTime = null; //时间域
        private ImageList imgArr;
        private IContainer components;
        public frmMedicine myMedicine = null;
        private Timer timer1;
        public event EventHandler ContentChanged;
        private int howScrollHeight = 0;
        private SelectForm medList = null;
        public frKnowledge knowledge = null; //知识库   
        public UcEditorControl()
        {
            try
            {
                InitializeComponent();
                this.myImageList = ZYKBTreeView.GetKBImageList();
                this.myEMRDoc = new ZYTextDocument();
                this.myEMRDoc.OwnerControl = this;
                this.myEMRDoc.OnContentChanged += new EventHandler(OnContentChanged);
                this.myEMRDoc.OnSelectionChanged += new EventHandler(this.ZYDoc_OnSelectionChanged);
                base.myDocument = this.myEMRDoc;
                this.myEMRDoc.RefreshSize();
                this.myEMRDoc.RefreshLine();
                ArrayList list = new ArrayList();
                ZYActionMenuItem toFirst = new ZYActionMenuItem("  导入");
                toFirst.DropDownItems.AddRange(new ZYActionMenuItem[] {
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("toxbs"),"主诉"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("toxbs"),"现病史"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("toxbs"),"既往史"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("toxbs"),"体格检查"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("toxbs"),"辅助检查")
                });
                //    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("toxbs"),"实验室检查和特殊检查结果"),
                //    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("toxbs"),"诊断依据及鉴别诊断"),
                //    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("toxbs"),"诊疗计划"),
                //    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("toxbs"),"病例特点"),
                //    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("toxbs"),"上级医师查房记录"),
                //});

                ZYActionMenuItem mi = new ZYActionMenuItem("表格操作");
                ZYActionMenuItem tableCellAlign = new ZYActionMenuItem("单元对齐");
                tableCellAlign.DropDownItems.AddRange(new ZYActionMenuItem[] {
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("tablecellaligntop"),"单元格居上"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("tablecellaligncenter"),"单元格居中"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("tablecellalignbottom"),"单元格居下"),
                });
                mi.DropDownItems.AddRange(new ZYActionMenuItem[] {
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("inserttable"), "插入表格") ,
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("tabledelete"), "删除表格"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("tablecolinsertleft"), "插入表格列(左)"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("tablecolinsertright"), "插入表格列(右)"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("tablerowinsertabove"), "插入表格行(上)"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("tablerowinsertbelow"), "插入表格行(下)"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("tablecellmerge"), "合并单元格"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("tablecoldelete"), "删除表格列"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("tablerowdelete"), "删除表格行"),
                    tableCellAlign,
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("tablerowheight"), "设置表格行高度"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("visibledelete"), "表格边框 隐藏/显示"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("lockCell"), "锁定单元格"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("tableLocked"), "锁定表格")
                });

                this.myPopupMenu = new ContextMenuStrip();
                this.myPopupMenu.Items.AddRange((new ToolStripItem[] 
                { 
                    //new ZYActionMenuItem(this.myEMRDoc.GetActionByName("undo"), "&U  撤销"),
                    //new ZYActionMenuItem(this.myEMRDoc.GetActionByName("redo"), "&R  重复"),                    
                    //new ToolStripSeparator(),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("diagnose"), "插入诊断(Ctrl+D)"),           
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("insertsign"),"特殊符号(Ctrl+S)"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("insertgongshi"), "插入公式(Ctrl+G)"), 
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("insertlischeckoutandcheck"),"插入LIS检验检查"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("insertpacs"),"插入PACS影像报告"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("insertdoctoradvice"),"插入病人医嘱"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("thesignature"), "插入上级医师签名"),
                    toFirst,
                    new ToolStripSeparator(),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("cut"), "剪切(Ctrl+X)"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("copy"), "复制(Ctrl+C)"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("paste"), "粘贴(Ctrl+V)"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("delete"), "删除"),                                                        
                    new ToolStripSeparator(),            
                    mi, //表格
                    new ToolStripSeparator(),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("texttime"),"插入时间"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("insertfixedinput"),"下划线输入框"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("insertknowledge"), "插入知识库"),
                    //new ZYActionMenuItem(this.myEMRDoc.GetActionByName("settextname"), "设置文书标题"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("checkbox"),"插入复选框"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("insertradiobutton"),"插入复选框2"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("checkWords"),"检查关键字(与性别冲突)"),
                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("insertimage"), "插入图片"),   

                    new ZYActionMenuItem(this.myEMRDoc.GetActionByName("insertSuperiorSignature"),"插入小模板"), 
                    //new ZYActionMenuItem(this.myEMRDoc.GetActionByName("insertmedicine"), "&Y  测试批注"),
                    //new ZYActionMenuItem(this.myEMRDoc.GetActionByName("insertsurgery"), "  插入手术"),               
                    //new ToolStripSeparator(),
                    //new ZYActionMenuItem(this.myEMRDoc.GetActionByName("inserttubebedsign"),"  插入管床医师签名"),
                   // new ZYActionMenuItem(this.myEMRDoc.GetActionByName("texttime"),"  插入时间"),
                   // new ZYActionMenuItem(this.myEMRDoc.GetActionByName("insertmedia"),"  插入多媒体"),
                   // new ZYActionMenuItem(this.myEMRDoc.GetActionByName("insertsign"),"&S  特殊符号(ctrl + s)"),
                   // new ToolStripSeparator(),
                   // new ZYActionMenuItem(this.myEMRDoc.GetActionByName("showall"), "&A  显示所有"),
                   // new ZYActionMenuItem(this.myEMRDoc.GetActionByName("enablesmarttag"), "&S  使用智能标签"),
                }));
                this.ContextMenuStrip = this.myPopupMenu;
                this.myPopupMenu.Opening += new CancelEventHandler(this.PopupMenu_Popup);
                this.myTagBox = new PictureBox();
                this.myTagBox.Size = new Size(this.SmartTagSize(), this.SmartTagSize());
                this.myTagBox.BorderStyle = BorderStyle.FixedSingle;
                this.myTagBox.Visible = false;
                this.myTagBox.BackColor = Color.White;
                this.myTagBox.Cursor = Cursors.Arrow;
                this.myTagBox.SizeMode = PictureBoxSizeMode.CenterImage;
                this.myTagBox.Click += new EventHandler(this.myTagBox_Click);
                this.myTagBox.MouseLeave += new EventHandler(this.myTagBox_MouseLeave);
                this.myTagBox.MouseEnter += new EventHandler(this.myTagBox_MouseEnter);
                this.myTagBox.MouseDown += new MouseEventHandler(this.myTagBox_MouseDown);
                this.myTagBox.MouseUp += new MouseEventHandler(this.myTagBox_MouseUp);
                base.Controls.Add(this.myTagBox);
                this.InitSmartTagProvider();
                this.myEMRDoc.InitZYVBEngine();
                this.mySelectedKb = new frKnowledgeSelect();
                this.myInputTimeUpdate = new frmInputTimeUpdate();
                this.myTime = new timeManager();
                this.myMedicine = new frmMedicine();
                this.Cursor = Cursors.IBeam;
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        public ZYTextElement my_lastElement = null;
        public void setToolTip(string head, string Message, string foot, Rectangle rec, ZYTextElement _moveElememnt)
        {
            if (this.myEMRDoc.Info.ShowAll)
            {
                if (_moveElememnt != my_lastElement)
                {
                    base.showTextTip.Hide();
                }
                if (_moveElememnt != my_lastElement)
                {
                    rec.Location = this.ViewPointToScreen(rec.X, rec.Y);
                    base.showTextTip.ShowRectangle = rec;
                    Message = Message.Replace("|", "\n");
                    base.showToolTip.SetSuperTooltip(base.showTextTip, new SuperTooltipInfo(head, foot, Message, null, null, DevComponents.DotNetBar.eTooltipColor.Default));
                    this.timer1.Start();
                    my_lastElement = _moveElememnt;
                }
            }
        }

        public void setToolTip(string Message, Rectangle rec, ZYTextElement _moveElememnt)
        {
            if (_moveElememnt != my_lastElement)
            {
                base.showTextTip.Hide();
            }
            if (_moveElememnt != my_lastElement)
            {
                rec.Location = this.ViewPointToScreen(rec.X, rec.Y);
                base.showTextTip.ShowRectangle = rec;
                Message = Message.Replace("|", "\n");
                base.showToolTip.SetSuperTooltip(base.showTextTip, new SuperTooltipInfo("提示", "", Message, null, null, DevComponents.DotNetBar.eTooltipColor.Default));
                this.timer1.Start();
                my_lastElement = _moveElememnt;
            }
        }

        public void HideToopTip()
        {
            base.showTextTip.Hide();
            this.my_lastElement = null;
        }

        public void ActiveSmartTag()
        {
            if (this.myTagBox.Visible)
            {
                this.myTagBox_Click(null, null);
            }
        }

        public void BindKBTreeView(ZYKBTreeView tvw)
        {
            if (tvw != null)
            {
                this.KBTreeView = tvw;
                this.KBTreeView.DesignKBMode = false;
                this.KBTreeView.EnableClickEvent = true;
                this.KBTreeView.DoubleClickMode = true;
                this.KBTreeView.KBItemClick += new KBItemClickHandler(this.KBTreeView_KBItemClick);
                this.KBTreeView.KBListClick += new KBListClickHandler(this.KBTreeView_KBListClick);
            }
        }

        public void CallOnResize()
        {
            this.OnResize(null);
        }

        private bool CanDrag(DragEventArgs drgevent)
        {
            return ((drgevent.Data.GetData(typeof(string)) is string) || ((drgevent.Data.GetData(typeof(KB_List)) is KB_List) || ((drgevent.Data.GetData(typeof(KB_Item)) is KB_Item) || (drgevent.Data.GetData(typeof(ArrayList)) is ArrayList))));
        }

        protected override void Dispose(bool disposing)
        {
            try
            {
                if (this.myTagMenu != null)
                {
                    this.myTagMenu.Dispose();
                    this.myTagMenu = null;
                }
                if (this.myKBPopupList != null)
                {
                    this.myKBPopupList.Dispose();
                    this.myKBPopupList = null;
                }
                if (this.myPopupMenu != null)
                {
                    this.myPopupMenu.Dispose();
                    this.myPopupMenu = null;
                }
                if (this.myImageList != null)
                {
                    this.myImageList.Dispose();
                    this.myImageList = null;
                }
                base.Dispose(disposing);
                if (this.myEMRDoc != null)
                {
                    this.myEMRDoc.Dispose();
                    this.myEMRDoc = null;
                }
            }
            catch
            {
            }
        }

        private void ZYDoc_OnSelectionChanged(object sender, EventArgs e)
        {
            this.UpdateSmartTag();
            this.Refresh();
        }

        public A_Action GetActionByName(string strName)
        {
            return this.myEMRDoc.GetActionByName(strName);
        }

        public string GetFinalText()
        {
            return this.myEMRDoc.GetFinalText();
        }

        public bool HandleCommand(string CommandName, string Param1, string Param2, string Param3)
        {
            ZYErrorReport.Instance.DebugPrint("执行HandleCommand[" + CommandName + "]参数1:" + Param1 + " 参数2:" + Param2 + " 参数3:" + Param3);
            foreach (A_Action action in this.myEMRDoc.Actions)
            {
                if ((action.ActionName() == CommandName) && action.isEnable())
                {
                    action.Param1 = Param1;
                    action.Param2 = Param2;
                    action.Param3 = Param3;
                    return action.UIExecute();
                }
            }
            ZYErrorReport.Instance.DebugPrint("未找到方法[" + CommandName + "]");
            return false;
        }

        public bool HandleCommandNoUI(string CommandName, string Param1, string Param2, string Param3)
        {
            ZYErrorReport.Instance.DebugPrint("执行HandleCommandNoUI[" + CommandName + "]参数1:" + Param1 + " 参数2:" + Param2 + " 参数3:" + Param3);
            foreach (A_Action action in this.myEMRDoc.Actions)
            {
                if ((action.ActionName() == CommandName) && action.isEnable())
                {
                    action.Param1 = Param1;
                    action.Param2 = Param2;
                    action.Param3 = Param3;
                    return action.Execute();
                }
            }
            ZYErrorReport.Instance.DebugPrint("未找到方法[" + CommandName + "]");
            return false;
        }

        public void HideSmartTag()
        {
            if (this.myTagBox.Visible)
            {
                this.myTagBox.Visible = false;
            }
        }

        internal void InitEventObject(ZYVBEventObject myEventObj)
        {
            if (myEventObj != null)
            {
                myEventObj.AltKey = CommonFunction.GetIntAttribute((int)Control.ModifierKeys, 0x40000);
                myEventObj.CtlKey = CommonFunction.GetIntAttribute((int)Control.ModifierKeys, 0x11);
                myEventObj.ShiftKey = CommonFunction.GetIntAttribute((int)Control.ModifierKeys, 0x10);
                myEventObj.ScreenX = Control.MousePosition.X;
                myEventObj.ScreenY = Control.MousePosition.Y;
                myEventObj.MouseButton = CommonFunction.GetIntAttribute((int)Control.MouseButtons, 0x100000) ? 1 : 2;
                myEventObj.ReturnValue = null;
                myEventObj.Cancel = false;
                Point point = base.PointToClient(Control.MousePosition);
                myEventObj.ClientX = point.X;
                myEventObj.ClientY = point.Y;
                point = base.PointToView(point.X, point.Y);
                myEventObj.X = point.X;
                myEventObj.Y = point.Y;
                myEventObj.CancelBubble = true;
            }
        }

        public bool InitForIE(string strCookie, string strPage, string strID, string strUserName)
        {
            try
            {
                ZYKBBuffer.Instance.Connection = this.myEMRDoc.DataSource.DBConn;
                base.bolRunInIE = true;
                this.strCurrentID = strID;
                this.strCurrentUserName = strUserName;
                XMLHttpConnection connection = new XMLHttpConnection();
                connection.IECookies = strCookie;
                connection.ConnectionString = strPage;
                connection.Open();
                this.myEMRDoc.PageSettings.PaperSize = new PaperSize("CUSTOM", 100, 100);
                this.myEMRDoc.DataSource.DBConn.Connection = connection;
                this.myEMRDoc.DataSource.DBConn.AutoOpen = false;
                this.myEMRDoc.Info.ID = strID;
                this.myEMRDoc.Info.ShowPageLine = false;
                this.myEMRDoc.UpdateViewSize();
                this.myEMRDoc.SaveLogs.CurrentUserName = strUserName;
                this.Refresh();
                return true;
            }
            catch (Exception exception)
            {
                ZYErrorReport.Instance.SourceException = exception;
                ZYErrorReport.Instance.SourceObject = this;
                ZYErrorReport.Instance.UserMessage = "加载电子病历文本编辑器错误";
                ZYErrorReport.Instance.MemberName = "ZYEditorControl.InitForIE";
                ZYErrorReport.Instance.ReportError();
            }
            return false;
        }

        public void InitKBPopupList()
        {
            if (this.myKBPopupList == null)
            {
                this.myKBPopupList = new ZYKBPopupList();
                this.myKBPopupList.AutoClose = true;
                this.myKBPopupList.OwnerEditor = this;
                this.myKBPopupList.OwnerDocument = this.myEMRDoc;
                this.myKBPopupList.ImageList = this.myImageList;
                base.myPopupList = this.myKBPopupList;
                this.myKBPopupList.AutoClose = false;
            }
            this.myKBPopupList.Title = "";
            this.myKBPopupList.CompositionRect = Rectangle.Empty;
        }

        public void HideControls()
        {
            if (mySelectedKb != null && mySelectedKb.Visible)
            {
                mySelectedKb.Hide();
            }

            if (myInputTimeUpdate != null && myInputTimeUpdate.Visible)
            {
                myInputTimeUpdate.Hide();
            }
            if (myTime != null && myTime.Visible)
            {
                myTime.Hide();
            }
            if (myMedicine != null && myMedicine.Visible)
            {
                myMedicine.Hide();
            }

            if (medList != null && medList.Visible)
            {
                medList.CloseList();
            }

            if (this.knowledge != null && this.knowledge.Visible)
            {
                this.knowledge.Hide();
            }


            if (Singleton<KnowledgeBase>.Instance.showKnowledgeControl != null
                && Singleton<KnowledgeBase>.Instance.showKnowledgeControl.Visible)
            {
                Singleton<KnowledgeBase>.Instance.showKnowledgeControl.Hide();
            }
        }

        protected override bool InitPopupList()
        {
            if (base.myPopupList == null)
            {
                this.myKBPopupList = new ZYKBPopupList();
                this.myKBPopupList.Clear();
                this.myKBPopupList.AutoClose = true;
                this.myKBPopupList.OwnerEditor = this;
                base.myPopupList = this.myKBPopupList;
            }
            return true;
        }

        /// <summary>
        /// 初始化智能提示
        /// </summary>
        private void InitSmartTagProvider()
        {
            this.myTagMenu = new ContextMenu();
            this.mySmartTagProviderList = new ArrayList();
            this.mySmartTagProviderList.Add(new TableTagProvider());
            this.mySmartTagProviderList.Add(new DivTagProvider());
            this.mySmartTagProviderList.Add(new ImageTagProvider());
            this.mySmartTagProviderList.Add(new KBTagProvider());
            this.mySmartTagProviderList.Add(new InputTagProvider());
            this.mySmartTagProviderList.Add(new ParagraphTagProvider());
            this.mySmartTagProviderList.Add(new HRuleTagProvider());
            this.mySmartTagProviderList.Add(new FixInputTagProvider());
            // this.mySmartTagProviderList.Add(new DivTagProviderUpdate());
            foreach (SmartTagProvider provider in this.mySmartTagProviderList)
            {
                provider.OwnerControl = this;
                provider.OwnerDocument = this.myEMRDoc;
            }
        }

        private void KBTreeView_KBItemClick(TreeNode TreeNode, KB_Item SelectedItem)
        {
            if (SelectedItem.isTemplate())
            {
                this.myEMRDoc._InsertDocument(SelectedItem.ItemValue);
            }
        }

        private void KBTreeView_KBListClick(TreeNode TreeNode, KB_List SelectedList)
        {
            this.myEMRDoc._InsertKB(SelectedList);
        }

        public bool LoadBase64XML(string strXML)
        {
            bool flag = true;
            try
            {
                if (StringCommon.isBlankString(strXML))
                {
                    this.myEMRDoc.ClearContent();
                }
                else
                {
                    string str = StringCommon.FromBase64String(strXML);
                    flag = this.LoadXML(str);
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
                flag = false;
            }
            if (base.bolRunInIE && flag)
            {
                this.myEMRDoc.Info.ID = this.strCurrentID;
                this.myEMRDoc.SaveLogs.CurrentUserName = this.strCurrentUserName;
                if (this.myEMRDoc.Marks.Count > 0)
                {
                    this.myEMRDoc.Info.ShowAll = true;
                    this.myEMRDoc.Info.ShowMark = true;
                    this.myEMRDoc.Info.LogicDelete = true;
                    this.myEMRDoc.RefreshElements();
                    this.myEMRDoc.RefreshLine();
                    this.myEMRDoc.UpdateView();
                }
            }
            return flag;
        }

        public bool LoadKBFromBuffer(string strKBBufferFile)
        {
            bool flag = false;
            if (StringCommon.isBlankString(strKBBufferFile))
            {
                strKBBufferFile = Application.StartupPath + @"\kbbuffer.xml";
            }
            try
            {
                if (!File.Exists(strKBBufferFile))
                {
                    goto Label_0072;
                }
                DateTime lastAccessTime = File.GetLastAccessTime(strKBBufferFile);
                if (DateTime.Now.Subtract(lastAccessTime).TotalHours >= 12.0)
                {
                    goto Label_0072;
                }
                XmlDocument document = new XmlDocument();
                document.Load(strKBBufferFile);
                ZYKBBuffer.Instance.LoadAllKBList(document.DocumentElement, false);
                flag = true;
            }
            catch
            {
            }
            return flag;
        Label_0072:
            return false;
        }

        public bool LoadKBFromDataBase()
        {
            this.strKBBufferFile = Application.StartupPath + @"\kbbuffer.xml";
            ZYKBBuffer.Instance.LoadAllKBList(false);
            XmlDocument document = new XmlDocument();
            document.LoadXml("<kb />");
            document.Save(this.strKBBufferFile);
            return true;
        }

        public bool LoadXML(string strXML)
        {
            XmlDocument document = new XmlDocument();
            this.myEMRDoc.BeginUpdate();
            this.myEMRDoc.ClearContent();
            document.PreserveWhitespace = true;
            document.LoadXml(strXML);
            this.myEMRDoc.FromXML(document.DocumentElement);
            this.myEMRDoc.ContentChanged();
            this.myEMRDoc.EndUpdate();
            this.myEMRDoc.Modified = false;
            this.Refresh();
            return true;
        }

        public bool LoadXMLFile(string strURL)
        {
            XmlDocument document = new XmlDocument();
            this.myEMRDoc.BeginUpdate();
            this.myEMRDoc.ClearContent();
            document.Load(strURL);
            this.myEMRDoc.FromXML(document.DocumentElement);
            this.myEMRDoc.ContentChanged();
            this.myEMRDoc.EndUpdate();
            this.myEMRDoc.Modified = false;
            this.Refresh();
            return true;
        }

        private void myTagBox_Click(object sender, EventArgs e)
        {
        }

        private void myTagBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (base.ShowingPopupList)
            {
                this.HideControls();
            }
            SmartTagProvider tag = this.myTagBox.Tag as SmartTagProvider;
            if (tag != null)
            {
                ArrayList myList = new ArrayList();
                tag.GetCommands(myList);
                if (myList.Count > 0)
                {
                    this.myTagMenu.MenuItems.Clear();
                    foreach (SmartTagItem item in myList)
                    {
                        this.myTagMenu.MenuItems.Add(new SmartTagMenuItem(item));
                    }
                    this.myTagBox.BackColor = Color.FromArgb(0x80, 0x80, 0xc0);
                    this.myTagMenu.Show(this, new Point(this.myTagBox.Left, this.myTagBox.Bottom));
                    this.myTagBox.BackColor = SystemColors.Window;
                }
            }
        }

        private void myTagBox_MouseEnter(object sender, EventArgs e)
        {
            this.myTagBox.BackColor = Color.FromArgb(0xb3, 0xb3, 0xd9);
        }

        private void myTagBox_MouseLeave(object sender, EventArgs e)
        {
            this.myTagBox.BackColor = SystemColors.Window;
        }

        private void myTagBox_MouseUp(object sender, MouseEventArgs e)
        {
            this.myTagBox_MouseEnter(null, null);
        }

        private void OnContentChanged(object sender, EventArgs e)
        {
            if (this.ContentChanged != null)
            {
                this.ContentChanged(sender, e);
            }
        }

        #region  拖动数据到控件内
        //protected override void OnDragDrop(DragEventArgs drgevent)
        //{
        //    
        //    string data = drgevent.Data.GetData(typeof(string)) as string;
        //    if (!StringCommon.isBlankString(data))
        //    {
        //        A_InsertString str2 = new A_InsertString();
        //        str2.OwnerDocument = this.myEMRDoc;
        //        if (str2.isEnable())
        //        {
        //            str2.Param1 = data;
        //            str2.Execute();
        //            this.UpdateTextCaret();
        //        }
        //    }
        //    else
        //    {
        //        KB_List myList = drgevent.Data.GetData(typeof(KB_List)) as KB_List;
        //        if (myList != null)
        //        {
        //            A_InsertKB tkb = new A_InsertKB();
        //            tkb.OwnerDocument = this.myEMRDoc;
        //            if (tkb.isEnable())
        //            {
        //                this.myEMRDoc._InsertKB(myList);
        //                this.UpdateTextCaret();
        //            }
        //        }
        //        else
        //        {
        //            KB_Item item = drgevent.Data.GetData(typeof(KB_Item)) as KB_Item;
        //            if (item != null)
        //            {
        //                if (item.isTemplate())
        //                {
        //                    if (this.RunInDesigner)
        //                    {
        //                        A_InsertKeyWord word = new A_InsertKeyWord();
        //                        word.OwnerDocument = this.myEMRDoc;
        //                        word.Param1 = item.ItemSEQ.ToString();
        //                        if (word.isEnable())
        //                        {
        //                            word.Execute();
        //                        }
        //                    }
        //                    else
        //                    {
        //                        this.myEMRDoc._InsertDocument(item.ItemValue);
        //                    }
        //                }
        //                else if (this.myEMRDoc.CanModify())
        //                {
        //                    ZYTextSelect newElement = new ZYTextSelect();
        //                    newElement.OwnerKBList = item.OwnerList;
        //                    newElement.Text = item.ItemText;
        //                    newElement.Value = item.ItemValue;
        //                    this.myEMRDoc._InsertElement(newElement);
        //                }
        //                this.UpdateTextCaret();
        //            }
        //            else
        //            {
        //                ArrayList list2 = drgevent.Data.GetData(typeof(ArrayList)) as ArrayList;
        //                if ((list2 != null) && this.myEMRDoc.CanModify())
        //                {
        //                    for (int i = 0; i < list2.Count; i++)
        //                    {
        //                        KB_List list3 = list2[i] as KB_List;
        //                        if (list3 != null)
        //                        {
        //                            A_InsertKB tkb2 = new A_InsertKB();
        //                            tkb2.OwnerDocument = this.myEMRDoc;
        //                            if (tkb2.isEnable())
        //                            {
        //                                this.myEMRDoc._InsertKB(list3);
        //                            }
        //                            this.UpdateTextCaret();
        //                            return;
        //                        }
        //                        KB_Item item2 = list2[i] as KB_Item;
        //                        if (item2 != null)
        //                        {
        //                            if (item2.isTemplate())
        //                            {
        //                                if (this.RunInDesigner)
        //                                {
        //                                    A_InsertKeyWord word2 = new A_InsertKeyWord();
        //                                    word2.OwnerDocument = this.myEMRDoc;
        //                                    word2.Param1 = item2.ItemSEQ.ToString();
        //                                    if (word2.isEnable())
        //                                    {
        //                                        word2.Execute();
        //                                    }
        //                                }
        //                                else
        //                                {
        //                                    this.myEMRDoc._InsertDocument(item2.ItemValue);
        //                                }
        //                            }
        //                            else
        //                            {
        //                                ZYTextSelect select2 = new ZYTextSelect();
        //                                select2.OwnerKBList = item2.OwnerList;
        //                                select2.Text = item2.ItemText;
        //                                select2.Value = item2.ItemValue;
        //                                this.myEMRDoc._InsertElement(select2);
        //                            }
        //                        }
        //                    }
        //                    this.UpdateTextCaret();
        //                }
        //                else
        //                {
        //                    base.OnDragDrop(drgevent);
        //                }
        //            }
        //        }
        //    }
        //}

        //protected override void OnDragEnter(DragEventArgs drgevent)
        //{
        //    if (this.CanDrag(drgevent))
        //    {
        //        drgevent.Effect = DragDropEffects.Copy;
        //    }
        //    base.OnDragEnter(drgevent);
        //}

        //protected override void OnDragOver(DragEventArgs drgevent)
        //{
        //    if (this.CanDrag(drgevent))
        //    {
        //        Point p = new Point(drgevent.X, drgevent.Y);
        //        p = base.PointToClient(p);
        //        p = base.PointToView(p.X, p.Y);
        //        this.myEMRDoc.Content.AutoClearSelection = true;
        //        base.ForceShowCaret = true;
        //        this.myEMRDoc.Content.MoveTo(p.X, p.Y);
        //        base.ForceShowCaret = false;
        //    }
        //    base.OnDragOver(drgevent);
        //}
        #endregion

        protected override void OnGotFocus(EventArgs e)
        {
            if (this.Focused)
            {
                base.bolMoveCaretWithScroll = false;
                this.UpdateTextCaret();
                base.bolMoveCaretWithScroll = true;
                base.OnGotFocus(e);
            }
        }

        protected override void OnResize(EventArgs e)
        {
            Point p = new Point(0);
            p = base.PointToScreen(p);
            if (!base.bolRunInIE)
            {
                base.OnResize(e);
                try
                {
                    this.myEMRDoc.UpdateTextCaret();
                    if (this.myTagBox.Visible && (this.myTagBox.Tag is SmartTagProvider))
                    {
                        this.ShowSmartTag(false);
                    }
                }
                catch
                { }
            }
        }

        protected override bool OnStartDrag(int x, int y)
        {
            return false;
        }

        private void PopupMenu_Popup(object sender, CancelEventArgs e)
        {
            for (int i = 0; i < this.myPopupMenu.Items.Count; i++)
            {
                if (this.myPopupMenu.Items[i] is ZYActionMenuItem)
                {
                    if (((ToolStripMenuItem)this.myPopupMenu.Items[i]).DropDownItems.Count > 0)
                    {

                        foreach (ZYActionMenuItem item2 in ((ToolStripMenuItem)this.myPopupMenu.Items[i]).DropDownItems)
                        {
                            item2.UpdateAction();
                            if (item2.DropDownItems.Count > 0)
                            {
                                foreach (ZYActionMenuItem item3 in item2.DropDownItems)
                                {
                                    item3.UpdateAction();
                                }
                            }
                        }
                    }
                    else
                    {
                        ((ZYActionMenuItem)this.myPopupMenu.Items[i]).UpdateAction();
                    }
                }
            }
        }

        /// <summary>
        /// 知识库选择
        /// </summary>
        /// <param name="myElement"></param>
        public void showKBList(ZYTextElement myElement)
        {
            this.mySelectedKb.setKnowLedge(myElement, this.myEMRDoc);
            this.mySelectedKb.Location =
                  this.mySelectedKb.updatePoint(this.ViewPointToScreen(myElement.RealLeft, myElement.RealTop), myElement.Height);
            this.mySelectedKb.Visible = true;
            this.mySelectedKb.setFocus();
        }

        public void showInputTime(ZYTextInput _input)
        {
            this.myInputTimeUpdate.setInputElement(_input);
            this.myInputTimeUpdate.Location =
                this.myInputTimeUpdate.updatePoint(this.ViewPointToScreen(_input.RealLeft, _input.RealTop), _input.Height);
            this.myInputTimeUpdate.Visible = true;
        }

        /// <summary>
        /// 时间选择
        /// </summary>
        /// <param name="myElement"></param>
        public void showTime(ZYTextElement myElement)
        {
            if (myElement is ZYTextTime)
            {
                this.myTime.setTime(myElement as ZYTextTime);
                this.myTime.StartPosition = FormStartPosition.Manual;
                this.myTime.Location =
                      this.myTime.updatePoint(this.ViewPointToScreen(myElement.RealLeft, myElement.RealTop), myElement.Height);
                this.myTime.Visible = true;
                this.myTime.setFocus();
            }
        }

        /// <summary>
        /// 药库选择
        /// </summary>
        /// <param name="myElement"></param>
        public void showMedicine(ZYTextElement myElement)
        {
            this.myMedicine.setMedicine(myElement as ZYTextMedicine);
            this.myMedicine.StartPosition = FormStartPosition.Manual;
            this.myMedicine.Location =
                this.myMedicine.updatePoint(this.ViewPointToScreen(myElement.RealLeft, myElement.RealTop), myElement.Height);
            this.myMedicine.Visible = true;
        }

        /// <summary>
        /// 知识库子元素
        /// </summary>
        /// <param name="myElement"></param>
        /// <returns></returns>
        public object ShowKBPopupList(ZYTextElement myElement)
        {
            base.bolCaptureMouse = false;
            bool multiple = false;
            this.InitKBPopupList();
            base.SetPopupPos(myElement.RealLeft, myElement.RealTop, myElement.Height);
            ZYTextSelect select = myElement as ZYTextSelect;
            if (select == null)
            {
                this.myKBPopupList.SetKBList((string)null);
            }
            else
            {
                this.myKBPopupList.FirstKBSEQ = select.ListSource;
                multiple = select.Multiple;
                this.myKBPopupList.MultiSelect = select.Multiple;
                if (select.HasOptions())
                {
                    this.myKBPopupList.SetKBList("", select.Options);
                }
                else if (select.OwnerKBList != null)
                {
                    this.myKBPopupList.SetKBList(select.OwnerKBList);
                }
                else
                {
                    this.myKBPopupList.SetKBList(select.ListSource);
                }
                this.myKBPopupList.SelectedText = select.Text;
                if (StringCommon.isBlankString(select.Name))
                {
                    this.myKBPopupList.Title = "[知识点]";
                }
                else
                {
                    this.myKBPopupList.Title = "[" + select.Name + "]";
                }
            }
            this.myKBPopupList.AutoClose = false;
            this.myKBPopupList.Show();
            base.Focus();
            base.ImeMode = ImeMode.Disable;
            if (select != null)
            {
                this.myKBPopupList.SelectedText = select.Text;
                this.myKBPopupList.SelectedStateChanged = select.GetSelectedStateChangedHandler();
                this.myKBPopupList.PopupSelection();
            }
            if (base.FindForm() != null)
            {
                base.FindForm().Activate();
            }
            object selectedObjects = null;
            while (this.myKBPopupList.WaitUserSelected())
            {
                if (this.myKBPopupList.MultiSelect)
                {
                    selectedObjects = this.myKBPopupList.SelectedObjects;
                    break;
                }
                ZYPopupList.ListItem selectedItem = this.myKBPopupList.SelectedItem;
                if (selectedItem != null)
                {
                    if (selectedItem.obj is KB_List)
                    {
                        this.myKBPopupList.MultiSelect = multiple;
                        this.myKBPopupList.SetKBList((selectedItem.obj as KB_List).SEQ);
                    }
                    else
                    {
                        selectedObjects = selectedItem.obj;
                        break;
                    }
                }
            }
            if (this.myKBPopupList != null)
            {
                this.myKBPopupList.CloseList();
            }
            base.ImeMode = ImeMode.On;
            // select.Deleteted = true;
            //  myEMRDoc .DrawDeleteLine (myEMRDoc .GetMarkLevel (select .DeleterIndex ),select .Bounds .Left ,select .Bounds .Top ,select .Width ,select .Height );\
            //    myEMRDoc._BackSpace();
            return selectedObjects;
        }

        public object ShowKBPopupList(ZYTextElement myElement, ArrayList myKBList, string strTitle, bool MultiSelect, bool MustKBItem)
        {
            this.InitKBPopupList();
            base.SetPopupPos(myElement.RealLeft, myElement.RealTop, myElement.Height);
            this.myKBPopupList.SetKBList(strTitle, myKBList);
            this.myKBPopupList.Show();
            base.Focus();
            this.myKBPopupList.SelectedIndex = 0;
            this.myKBPopupList.MultiSelect = false;
            base.ImeMode = ImeMode.Disable;
            object selectedObject = null;
            if (MustKBItem)
            {
                selectedObject = this.myKBPopupList.WaitForUserSelectKBItem(MultiSelect);
            }
            else
            {
                if (this.myKBPopupList.WaitUserSelected())
                {
                    selectedObject = this.myKBPopupList.SelectedObject;
                }
                this.myKBPopupList.CloseList();
            }
            base.ImeMode = ImeMode.On;
            return selectedObject;
        }

        internal void ShowSmartTag(bool Scroll)
        {
            if (!this.bolEnableSmartTag)
            {
                this.myTagBox.Visible = false;
            }
            else
            {
                SmartTagProvider tag = this.myTagBox.Tag as SmartTagProvider;
                if (tag != null)
                {
                    Point pos = tag.GetPos();
                    pos = base.ViewPointToClient(pos.X, pos.Y);
                    this.myTagBox.Location = pos;
                    this.myTagBox.Image = this.myImageList.Images[tag.ImageIndex];
                    this.myTagBox.BackColor = SystemColors.Window;
                    this.myTagBox.Visible = true;
                    if (Scroll)
                    {
                        //base.ScrollControlIntoView(this.myTagBox);
                    }
                }
            }
        }

        public int SmartTagSize()
        {
            return 0x16;
        }

        public string ToBase64ZYXML()
        {
            XmlDocument myDoc = new XmlDocument();
            this.myEMRDoc.Info.SaveMode = 3;
            this.myEMRDoc.ToXMLDocument(myDoc);
            return StringCommon.ToBase64String(myDoc.DocumentElement.OuterXml);
        }

        public string ToBase64Image(string strImageFormat)
        {
            ImageFormat bmp;
            strImageFormat = strImageFormat.ToLower();
            switch (strImageFormat)
            {
                case "bmp":
                    bmp = ImageFormat.Bmp;
                    break;

                case "emf":
                    bmp = ImageFormat.Emf;
                    break;

                case "exif":
                    bmp = ImageFormat.Exif;
                    break;

                case "gif":
                    bmp = ImageFormat.Gif;
                    break;

                case "icon":
                    bmp = ImageFormat.Icon;
                    break;

                case "jpg":
                case "jpeg":
                    bmp = ImageFormat.Jpeg;
                    break;

                case "png":
                    bmp = ImageFormat.Png;
                    break;

                case "tiff":
                    bmp = ImageFormat.Tiff;
                    break;

                case "wmf":
                    bmp = ImageFormat.Wmf;
                    break;

                default:
                    bmp = ImageFormat.Gif;
                    break;
            }
            Bitmap preViewImage = this.myEMRDoc.GetPreViewImage();
            preViewImage.Save(@"c:\preImage.bmp", bmp);
            return StringCommon.ImageToBase64String(preViewImage, bmp);
        }

        public string ToBase64XML()
        {
            XmlDocument myDoc = new XmlDocument();
            this.myEMRDoc.Info.SaveMode = 0;
            this.myEMRDoc.ToXMLDocument(myDoc);
            return StringCommon.ToBase64String(myDoc.DocumentElement.OuterXml);
        }

        public string ToBase64XML2()
        {
            XmlDocument myDoc = new XmlDocument();
            this.myEMRDoc.Info.SaveMode = 2;
            this.myEMRDoc.ToXMLDocument(myDoc);
            return StringCommon.ToBase64String(XMLCommon.CreateChildElement(myDoc.DocumentElement, "body", false).OuterXml);
        }

        internal void UpdateSmartTag()
        {
            if (!this.myEMRDoc.Loading && this.bolEnableSmartTag)
            {
                ZYTextElement currentElement = this.myEMRDoc.Content.CurrentElement;
                if (((currentElement != null) && !currentElement.Deleteted))
                {
                    if (!currentElement.Parent.Locked)
                    {
                        foreach (SmartTagProvider provider in this.mySmartTagProviderList)
                        {
                            provider.Element = currentElement;
                            if (provider.isEnable())
                            {
                                this.myTagBox.Tag = provider;
                                this.ShowSmartTag(true);
                                return;
                            }
                        }
                    }
                    else
                    {
                        SmartTagProvider provider = (SmartTagProvider)this.mySmartTagProviderList[this.mySmartTagProviderList.Count - 1];
                        provider.Element = currentElement;
                        if (provider.isEnable())
                        {
                            this.myTagBox.Tag = provider;
                            this.ShowSmartTag(true);
                            return;
                        }
                    }
                }
                if (this.myTagBox.Visible)
                {
                    this.myTagBox.Visible = false;
                }
            }
            else if (this.myTagBox.Visible)
            {
                this.myTagBox.Visible = false;
            }
        }

        public void UpdateTextCaret()
        {
            ZYTextElement myElement = this.myEMRDoc.Content.CurrentElement;
            if (this.myEMRDoc.Content.isDragMove)
            {
                myElement = this.myEMRDoc.horeELement;
            }
            if (myElement != null)
            {
                if (this.myEMRDoc.Content.LineEndFlag)
                {
                    myElement = this.myEMRDoc.Content.PreElement;
                    if (myElement != null)
                    {
                        base.MoveTextCaretTo(myElement.RealLeft + myElement.Width, myElement.RealTop + myElement.Height, myElement.Height);
                    }
                }
                else
                {
                    base.MoveTextCaretTo(myElement.RealLeft, myElement.RealTop + myElement.Height, myElement.Height);
                }
            }
        }

        public void UpdateTextCaret(ZYTextElement myElement)
        {
            if (myElement != null)
            {
                if (this.myEMRDoc.Content.LineEndFlag)
                {
                    myElement = this.myEMRDoc.Content.PreElement;
                    if (myElement != null)
                    {
                        base.MoveTextCaretTo(myElement.Bounds.Right, myElement.Bounds.Bottom, myElement.Height);
                    }
                }
                else
                {
                    base.MoveTextCaretTo(myElement.Bounds.Left, myElement.Bounds.Bottom, myElement.Height);
                }
            }
        }

        protected override ImeMode DefaultImeMode
        {
            get
            {
                if ((this.myKBPopupList != null) && this.myKBPopupList.Visible)
                {
                    return ImeMode.Off;
                }
                return base.DefaultImeMode;
            }
        }

        public ZYTextDocument EMRDoc
        {
            get
            {
                return this.myEMRDoc;
            }
        }

        public bool EnableSmartTag
        {
            get
            {
                return this.bolEnableSmartTag;
            }
            set
            {
                this.bolEnableSmartTag = value;
            }
        }

        public bool HasHScroll
        {
            get
            {
                return base.HScroll;
            }
        }

        public bool HasVScroll
        {
            get
            {
                return base.VScroll;
            }
        }

        public System.Windows.Forms.ImageList ImageList
        {
            get
            {
                return this.myImageList;
            }
        }

        public ImageList ImgList
        {
            get
            {
                return this.ImgList;
            }
        }

        public ZYKBPopupList KBPopupList
        {
            get
            {
                return this.myKBPopupList;
            }
        }

        public string SelectedText
        {
            get
            {
                if (this.EMRDoc != null)
                {
                    return this.EMRDoc.Content.GetSelectedText();
                }
                return "";
            }
        }

        public ArrayList SmartTagProviderList
        {
            get
            {
                return this.mySmartTagProviderList;
            }
        }



        public class ZYActionMenuItem : ToolStripMenuItem
        {
            private A_Action myAction = null;

            public ZYActionMenuItem(A_Action a, string strText)
            {
                this.Action = a;
                base.Text = strText;
            }

            public ZYActionMenuItem(string strText)
            {
                base.Text = strText;
            }

            protected override void OnClick(EventArgs e)
            {
                if (this.myAction != null && this.myAction.ActionName() == "toxbs")
                {
                    this.Action.Param2 = this.Text;
                }
                if ((this.myAction != null) && this.myAction.isEnable())
                {
                    this.myAction.Param1 = base.Checked ? "0" : "1";
                    this.myAction.UIExecute();
                }
                else
                {
                    base.OnClick(e);
                }
            }

            public void UpdateAction()
            {
                if (this.myAction != null)
                {
                    base.Enabled = this.myAction.isEnable();
                    if (this.myAction.ActionName() == "insertSuperiorSignature")
                    {
                        base.Visible = base.Enabled;
                    }
                    if (this.myAction.ActionName() == "toxbs" && this.myAction.OwnerDocument.Us.TextKind_id != 126 && this.Text == "上级医师查房记录")
                    {
                        base.Enabled = false;
                    }
                    if (this.myAction.CheckState() == 1)
                    {
                        base.Checked = true;
                    }
                    else
                    {
                        base.Checked = false;
                    }
                }
            }
            public A_Action Action
            {
                get
                {
                    return this.myAction;
                }
                set
                {
                    this.myAction = value;
                    this.UpdateAction();
                }
            }
        }

        internal class SmartTagMenuItem : MenuItem
        {
            private SmartTagItem myItem = null;

            public SmartTagMenuItem(SmartTagItem vItem)
            {
                this.Item = vItem;
            }

            protected override void OnClick(EventArgs e)
            {
                if ((this.myItem != null) && (this.myItem.Provider != null))
                {
                    this.myItem.Provider.HandleCommand(this.myItem);
                }
            }

            public SmartTagItem Item
            {
                set
                {
                    if (value != null)
                    {
                        this.myItem = value;
                        base.Text = value.Text;
                        base.Checked = value.Checked;
                    }
                }
            }
        }

        /// <summary>
        /// 续打定位
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if ((e.Button == MouseButtons.Left) && this.myEMRDoc.EnableJumpPrint)
            {
                int pos = 0;
                ZYTextElement currentElement = this.myEMRDoc.Content.CurrentElement;
                this.myEMRDoc.JumpElement = currentElement;
                pos = this.ViewPointToClient(currentElement.RealLeft, currentElement.RealTop).Y;

                if (pos >= 0)
                {
                    if ((pos > 0) && (this.myEMRDoc.JumpPrintPosition != pos))
                    {
                        this.myEMRDoc.JumpPrintPosition = pos;
                        this.howScrollHeight = base.AutoScrollPosition.Y;
                        this.Refresh();
                    }
                }
            }
        }

        /// <summary>
        /// 跨越页数
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        public int getPages(int pos)
        {
            int marignTop = 0;
            int howTop = 0;

            if (this.myEMRDoc.Pages.Count > 0)
            {
                for (int i = 0; i < this.myEMRDoc.Pages.Count; i++)
                {
                    howTop += this.myEMRDoc.Pages[i].Height;
                    if (i != 0)
                        marignTop += this.Pages.TopMargin - (this.myEMRDoc.Pages[i - 1].Height - this.Pages.Height);
                    if (pos < this.Pages.Height - marignTop - 16)
                    {
                        this.myEMRDoc.PrintPageIndex = 0;
                        if (pos > howTop)
                            this.myEMRDoc.PrintPageTop = 0;
                        else
                            this.myEMRDoc.PrintPageTop = this.myEMRDoc.Pages[i].Height - (howTop - pos);

                        return 0;
                    }
                    else if (pos - 16 <= howTop && i >= 1 && pos - 16 > howTop - this.myEMRDoc.Pages[i].Height)
                    {
                        if (pos > howTop)
                            this.myEMRDoc.PrintPageTop = 0;
                        else
                            this.myEMRDoc.PrintPageTop = this.myEMRDoc.Pages[i].Height - (howTop - pos - 16);

                        this.myEMRDoc.PrintPageIndex = i;
                        return marignTop;
                    }
                    else
                    {
                        if (pos > howTop)
                            this.myEMRDoc.PrintPageTop = 0;
                    }
                }
            }
            return 0;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (base.DesignMode)
            {
                using (StringFormat f = new StringFormat())
                {
                    e.Graphics.Clear(SystemColors.AppWorkspace);
                    f.Alignment = StringAlignment.Center;
                    f.LineAlignment = StringAlignment.Center;
                    e.Graphics.DrawString(base.Name + Environment.NewLine + base.GetType().Name, Control.DefaultFont, Brushes.Red, new RectangleF(0f, 0f, (float)base.ClientSize.Width, (float)base.ClientSize.Height), f);
                }
            }
            else
            {
                base.OnPaint(e);
                if (this.myEMRDoc.EnableJumpPrint && (this.myEMRDoc.JumpPrintPosition > 0))
                {
                    base.DrawJumpPrintArea(e.Graphics, e.ClipRectangle, this.myEMRDoc.JumpPrintPosition, this.howScrollHeight);
                }
            }
        }

        protected void OnViewPaint(PaintEventArgs e, SimpleRectangleTransform trans)
        {
            //if (!base.Updating)
            //{
            this.myEMRDoc.PageIndex = trans.PageIndex;
            if (trans.Flag2 == 0)
            {
                this.myEMRDoc.DrawHead(e.Graphics, trans.DescRect);
            }
            else if (trans.Flag2 == 1)
            {
                this.myEMRDoc.DrawDocument(e.Graphics, e.ClipRectangle);
            }
            else if (trans.Flag2 == 2)
            {
                this.myEMRDoc.DrawFooter(e.Graphics, trans.DescRect);
            }
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.imgArr = new System.Windows.Forms.ImageList(this.components);
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // imgArr
            // 
            this.imgArr.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
            this.imgArr.ImageSize = new System.Drawing.Size(16, 16);
            this.imgArr.TransparentColor = System.Drawing.Color.Transparent;
            // 
            // timer1
            // 
            this.timer1.Interval = 500;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // UcEditorControl
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScroll = false;
            this.Name = "UcEditorControl";
            this.Size = new System.Drawing.Size(186, 162);
            this.ResumeLayout(false);

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            if (my_lastElement != null)
                base.showTextTip.Show();
        }
    }
}

